<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">设置公司信息</block>
		</cu-custom>
		<view class="login-bg">
			<view class="cu-list menu login-card">
				<view class="login-head">设置公司信息</view>
				<view class="login-input">
					<input type="text" v-model="company" placeholder="设置公司名" />
				</view>
				<view class="cu-item">
					<text class="uni-list-cell-left">
						上下班时间
					</text>
					<view>
						<picker style="margin-left: 100upx;" mode="time" :value="starttime" @change="bindSTimeChange">
							<view class="uni-input">{{starttime}}</view>
						</picker>
					</view>
					——
					<view>
						<picker mode="time" :value="endtime" @change="bindETimeChange">
							<view class="uni-input">{{endtime}}</view>
						</picker>
					</view>
				</view>
				
				<view class="cu-item">
					<text class="uni-list-cell-left">
						休息时间
					</text>
					<view>
						<picker style="margin-left: 130upx;" mode="time" :value="resttimebegin" @change="bindSrestTimeChange">
							<view class="uni-input">{{resttimebegin}}</view>
						</picker>
					</view>
					——
					<view>
						<picker mode="time" :value="resttimeend" @change="bindErestTimeChange">
							<view class="uni-input">{{resttimeend}}</view>
						</picker>
					</view>
				</view>
			</view>
		</view>

		<view class="login-btn">
			<button class="landing" @click="register" type="primary">下一步</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				password: '',
				company: '',
				phone: '',
				starttime: '09:00',
				endtime: '18:00',
				resttimebegin: '12:00',
				resttimeend: '13:00'
			}
		},
		onLoad(res) {
			this.phone = res.phone
			this.password = res.password
		},
		methods: {
			register() {

				var attworkinghours = this.countAttTime()
				console.log(attworkinghours)
				/** 测试  start **/
				// uni.navigateTo({
				// 	url: '../device/increase'
				// });
				// return

				/** end **/
				console.log("phone = " + this.phone)
				console.log("password = " + this.password)
				console.log("company = " + this.company)

				if (this.company == '') {
					uni.showToast({
						title: "请填写公司名",
						icon: "none"
					})
					return
				}
				
				//开始注册
				var that = this
				uni.request({
					url: this.$constant.BASEURL + this.$constant.REGISTER,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						mobile: this.phone,
						departname: this.company,
						password: this.password,
						stime: this.starttime,
						etime: this.endtime,
						resttimebegin: this.resttimebegin,
						resttimeend: this.resttimeend,
						attworkinghours: attworkinghours
					},
					success: (res) => {
						console.log(res.data)
						if (res.data.code == 0) {
							uni.showToast({
								title: "注册成功",
							})
							//注册成功后直接登陆
							that.autoLogin()
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none"
							})
						}
					}
				});
			},
			checkPass(password) {
				return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(password)
			},
			autoLogin() {
				uni.request({
					url: this.$constant.BASEURL + this.$constant.USER_LOGIN,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: this.phone,
						password: this.password
					},
					success: (res) => {
						var result = res.data
						console.log(result)
						if (result.code == 0) {
							//保存用户信息
							getApp().globalData.username = result.data.username
							getApp().globalData.departname = result.data.departname
							getApp().globalData.token = result.data.token
							getApp().globalData.orgcode = result.data.orgcode
							uni.showToast({
								title: '登录成功',
							})
							uni.redirectTo({
								url: '../device/increase'
							})
						} else {
							uni.showToast({
								title: result.message,
								icon: 'none'
							})
							uni.navigateBack({
								delta: 1
							});
						}
					}
				});
			},
			bindSTimeChange: function(e) {
				this.starttime = e.target.value
			},
			bindETimeChange: function(e) {
				this.endtime = e.target.value
			},
			bindSrestTimeChange: function(e) {
				this.resttimebegin = e.target.value
			},
			bindErestTimeChange: function(e) {
				this.resttimeend = e.target.value
			},
			countAttTime() {
				//计算时间
				var attworkinghours = 0
				var sTime = new Date(`1994-01-01 ${this.starttime}`).getTime()
				var eTime = new Date(`1994-01-01 ${this.endtime}`).getTime()

				var resStime = new Date(`1994-01-01 ${this.resttimebegin}`).getTime()
				var resEtime = new Date(`1994-01-01 ${this.resttimeend}`).getTime()

				var attworkinghours = this.millisToHours(eTime - sTime)
				if (attworkinghours < 0) {
					attworkinghours += 24
				}
				var restTime = this.millisToHours(resEtime - resStime)
				
				if(resStime-sTime < 0){
					uni.showToast({
						title: "休息开始时间不能大于上班时间",
						icon: "none"
					})
					return
				}
				
				if(resStime-sTime < 0){
					uni.showToast({
						title: "休息开始时间不能大于上班时间",
						icon: "none"
					})
					return
				}
				
				if(restTime<0){
					uni.showToast({
						title: "休息开始时间不能大于结束时间",
						icon: "none"
					})
					return
				}
				
				attworkinghours -= restTime
				return attworkinghours
			},
			millisToHours(millis) {
				return (millis / 1000 / (60 * 60))
			}
		}
	}
</script>

<style>
	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #5599ff, #5599ff);
	}

	.login-btn {
		padding: 10upx 20upx;
		margin-top: 350upx;
	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: left;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: right;
		font-size: 26upx;

	}

	.login-input input {
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		min-height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;
	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}

	.login-card {
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.12);
		position: relative;
		margin-top: 120upx;
	}

	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#5599ff, #5599ff);
	}
</style>
